#include "fl_ntctpg.h"

#include "flagdescriptor.h"
#include "QtSerialization.h"

ExaFileObj fl_NtcTpg::m_file_obj = EXA_FILE_OBJ_TOPOGRAPHY;

fl_NtcTpg::fl_NtcTpg ( void )
{
    setFlag(fl_NtcTpg::NONE);
}

fl_NtcTpg::fl_NtcTpg ( const int& tpg )
{
    setFlag(tpg);
}

fl_NtcTpg::fl_NtcTpg ( const fl_NtcTpg& fl )
{
    m_intflag = fl.m_intflag;
    m_set = fl.m_set;
}

fl_NtcTpg & fl_NtcTpg::operator = ( const fl_NtcTpg& fl )
{
    m_intflag = fl.m_intflag;
    m_set = fl.m_set;
    return *this;
}

void fl_NtcTpg::setFlag(const int& tpg)
{
    if (
            tpg == fl_NtcTpg::T1 ||
            tpg == fl_NtcTpg::T2 ||
            tpg == fl_NtcTpg::T3 ||
            tpg == fl_NtcTpg::T4 ||
            tpg == fl_NtcTpg::NONE
        )
    {
        m_intflag = tpg;
    }
    else
    {
        m_intflag = fl_NtcTpg::NONE;
    }
}

string fl_NtcTpg::getFlagStr(void) const
{
    switch (m_intflag)
    {
    case fl_NtcTpg::T1:
        return("T1");
    case fl_NtcTpg::T2:
        return("T2");
    case fl_NtcTpg::T3:
        return("T3");
    case fl_NtcTpg::T4:
        return("T4");
    case fl_NtcTpg::NONE:
        return("NONE");
    }
}

bool   fl_NtcTpg::setFlag(const string& str)
{
    if (str.compare("T1")==0)
    {
        m_intflag = fl_NtcTpg::T1;
        m_set = true;
    }
    else if (str.compare("T2")==0)
    {
        m_intflag = fl_NtcTpg::T2;
        m_set = true;
    }
    else if (str.compare("T3")==0)
    {
        m_intflag = fl_NtcTpg::T3;
        m_set = true;
    }
    else if (str.compare("T4")==0)
    {
        m_intflag = fl_NtcTpg::T4;
        m_set = true;
    }
    else if (str.compare("NONE")==0)
    {
        m_intflag = fl_NtcTpg::NONE;
        m_set = false;
    }
    else
    {
        m_intflag = fl_NtcTpg::NONE;
        m_set = false;
        return(false);
    }
    return(true);
}

string fl_NtcTpg::getFlagDescription( ExaTextFormat format ) const
{
    return FlagDescriptor::getFlagDescription( *this, format );
}

ExaFileObj fl_NtcTpg::fileObj ( void ) const
{
    return m_file_obj;
}

ExaFileErr fl_NtcTpg::fileOpen (const string& file_name)
{
    ExaFileErr file_err;
    file_err = fileControl(file_name,EXA_FILE_OM_READ_ONLY);

    if ( file_err == EXA_FILE_ERR_OK )
    {
        quint32 magic_obj = fileMagic();
        switch (magic_obj)
        {
            case EXA_MN_1_0_0_BIN_TPG:
            {
                /**************************************************/
                /*           FILE BINARIO SLS V 100               */
                /**************************************************/
                // apro intestazione dati binari
                QFile file(QSTR(file_name));
                if (file.open(QIODevice::ReadOnly))
                {
                    QDataStream in(&file);
                    in.setVersion(QDataStream::Qt_4_5);
                    quint32 magic_file, version_file;
                    in >> magic_file >> version_file;

                    // carico l'oggetto
                    in >> *this;
                }
                else
                {
                    return EXA_FILE_ERR_GENERIC;
                }
                /**************************************************/
            } break;
            default:
                return EXA_FILE_ERR_MN_UNKNOWN;
        }
    }
    fileReset();
    return file_err;
}

ExaFileErr fl_NtcTpg::fileSave (const string& file_name) const
{
    ExaFileErr file_err;
    file_err = fileControl(file_name,EXA_FILE_OM_WRITE_ONLY);

    if ( file_err == EXA_FILE_ERR_OK )
    {
        quint32 magic_obj = fileMagic();
        quint32 version_obj = fileVersion();

        switch (magic_obj)
        {
            case EXA_MN_1_0_0_BIN_TPG:
            {
                /**************************************************/
                /*           FILE BINARIO SLS V 100               */
                /**************************************************/
                // apro intestazione dati binari
                QFile file(QSTR(file_name));
                if (file.open(QIODevice::WriteOnly))
                {
                    // salvo i dati binari
                    QDataStream out(&file);
                    out.setVersion(QDataStream::Qt_4_5);
                    out << magic_obj << version_obj << *this ;
                }
                else
                {
                    return EXA_FILE_ERR_GENERIC;
                }
                /**************************************************/
            } break;
            default:
                return EXA_FILE_ERR_MN_UNKNOWN;
        }
    }
    fileReset();
    return file_err;
}
